package com.youku
{
   import flash.display.MovieClip;
   import flash.events.Event;
   import flash.events.FullScreenEvent;
   import com.youku.events.ViewManagerEvent;
   import flash.display.StageDisplayState;
   
   public class ViewManager extends MovieClip
   {
      
      public function ViewManager() {
         super();
         this.init();
      }
      
      protected var _background:MovieClip;
      
      protected var _videoLayer:MovieClip;
      
      protected var _videoOverLayer:MovieClip;
      
      protected var _pluginLayer:MovieClip;
      
      protected var _adLayer:MovieClip;
      
      protected var _controlsLayer:MovieClip;
      
      private var _dispatchResizeEvent:Boolean = false;
      
      private var _lastStageWidth:Number = 0;
      
      private var _lastStageHeight:Number = 0;
      
      private var _setWidth:Number = 0;
      
      private var _setHeight:Number = 0;
      
      private var _userSetSize:Boolean = false;
      
      private function init() : void {
         this._background = new MovieClip();
         this._videoLayer = new MovieClip();
         this._videoOverLayer = new MovieClip();
         this._pluginLayer = new MovieClip();
         this._adLayer = new MovieClip();
         this._controlsLayer = new MovieClip();
         addChild(this._background);
         addChild(this._videoLayer);
         addChild(this._videoOverLayer);
         addChild(this._pluginLayer);
         addChild(this._adLayer);
         addChild(this._controlsLayer);
         this.addEventListener(Event.ADDED_TO_STAGE,this.onAddedToStage);
      }
      
      private function onAddedToStage(param1:Event) : void {
         removeEventListener(Event.ADDED_TO_STAGE,this.onAddedToStage);
         stage.addEventListener(FullScreenEvent.FULL_SCREEN,this.onFullScreenChange);
         stage.addEventListener(Event.RESIZE,this.onStageSizeChange);
         stage.addEventListener(Event.ENTER_FRAME,this.onEnterFrame);
      }
      
      private function onFullScreenChange(param1:FullScreenEvent) : void {
         PlayerConfig.isFullScreen = param1.fullScreen;
         dispatchEvent(new ViewManagerEvent(ViewManagerEvent.FULLSCREEN_MODE_CHANGED,param1.fullScreen));
      }
      
      public function changeFullScreen() : void {
         try
         {
            if(stage.displayState == StageDisplayState.NORMAL)
            {
               stage.displayState = StageDisplayState.FULL_SCREEN;
            }
            else if(stage.displayState == StageDisplayState.FULL_SCREEN)
            {
               stage.displayState = StageDisplayState.NORMAL;
            }
            
         }
         catch(e:Error)
         {
         }
      }
      
      public function zoomOut() : void {
         try
         {
            if(stage.displayState == StageDisplayState.FULL_SCREEN)
            {
               stage.displayState = StageDisplayState.NORMAL;
            }
         }
         catch(e:Error)
         {
         }
      }
      
      private function onStageSizeChange(param1:Event) : void {
         this._dispatchResizeEvent = true;
      }
      
      private function onEnterFrame(param1:Event) : void {
         if(!(stage == null) && ((this._dispatchResizeEvent) || !(stage.stageWidth == this._lastStageWidth) || !(stage.stageHeight == this._lastStageHeight)))
         {
            if(!this._userSetSize && (!(stage.stageWidth == this._lastStageWidth) || !(stage.stageHeight == this._lastStageHeight)) || (this._userSetSize) && (!(this._setHeight == 0) || !(this._setWidth == 0)))
            {
               this.tryToDispatchResize();
            }
         }
      }
      
      private function tryToDispatchResize() : void {
         var _loc1_:* = false;
         if((this._userSetSize) || (!(stage.stageWidth == this._lastStageWidth) || !(stage.stageHeight == this._lastStageHeight)) && stage.stageHeight > 0 && stage.stageWidth > 0)
         {
            if(this._userSetSize)
            {
               this._lastStageHeight = this._setHeight;
               this._lastStageWidth = this._setWidth;
               this._setHeight = 0;
               this._setWidth = 0;
            }
            else
            {
               this._lastStageHeight = stage.stageHeight;
               this._lastStageWidth = stage.stageWidth;
            }
            PlayerConfig.normalWidth = this._lastStageWidth;
            PlayerConfig.normalHeight = this._lastStageHeight;
            _loc1_ = false;
            if(stage.displayState == StageDisplayState.NORMAL)
            {
               _loc1_ = false;
            }
            else if(stage.displayState == StageDisplayState.FULL_SCREEN)
            {
               _loc1_ = true;
            }
            
            dispatchEvent(new ViewManagerEvent(ViewManagerEvent.PLAYER_SIZE_CHANGED,_loc1_));
         }
         this._dispatchResizeEvent = false;
      }
      
      public function getInitialSize() : void {
         PlayerConfig.normalWidth = stage.stageWidth;
         PlayerConfig.normalHeight = stage.stageHeight;
      }
      
      public function get videoLayer() : MovieClip {
         return this._videoLayer;
      }
      
      public function get videoOverLayer() : MovieClip {
         return this._videoOverLayer;
      }
      
      public function get adLayer() : MovieClip {
         return this._adLayer;
      }
      
      public function get pluginLayer() : MovieClip {
         return this._pluginLayer;
      }
      
      public function get controlsLayer() : MovieClip {
         return this._controlsLayer;
      }
      
      public function get background() : MovieClip {
         return this._background;
      }
      
      public function get isInFullScreen() : Boolean {
         if(stage == null)
         {
            return false;
         }
         if(stage.displayState == StageDisplayState.FULL_SCREEN)
         {
            return true;
         }
         return false;
      }
      
      public function setSize(param1:Number, param2:Number) : void {
         this._setWidth = param1;
         this._setHeight = param2;
         this._userSetSize = true;
         this.tryToDispatchResize();
      }
   }
}
